package com.acme.bankapp.networking;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.service.bank.Configuration;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Student on 19.11.2014.
 */
public class BankServer {

    public static void startServer(Bank bank) {
        System.out.println("Begin listening port ");
        try(ServerSocket ss = new ServerSocket(Configuration.getPort());
            Socket s = ss.accept()){

            try(ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
                DataInputStream in = new DataInputStream(s.getInputStream());) {

                String request = null;
                while (!((request = in.readUTF()).equals("halt"))) {
                    switch (request) {
                        case "getClients":
                            out.writeObject(bank.getClients());
                            break;

                        default:
                            out.writeObject("Unknown command");
                    }
                }

            }catch (IOException e) {
                e.printStackTrace();
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
